package com.kalix.training.live.biz;

import com.kalix.framework.core.api.persistence.JsonData;
import com.kalix.framework.core.api.persistence.JsonStatus;
import com.kalix.framework.core.impl.biz.ShiroGenericBizServiceImpl;
import com.kalix.training.live.api.biz.ILiveBeanService;
import com.kalix.training.live.api.dao.ILiveBeanDao;
import com.kalix.training.live.entities.LiveBean;
import com.kalix.training.live.entities.LiveRoomBean;

import java.text.SimpleDateFormat;
import java.util.*;

public class LiveBeanServiceImpl extends ShiroGenericBizServiceImpl<ILiveBeanDao, LiveBean>
        implements ILiveBeanService {

    /**
    * @Author lwj
    * @Description 开始直播
    * @Date 下午 4:17 2019/1/23 0023
    **/
    public JsonData  startLive(Long userId,String username,String type, String imgUrl,String liveName){
        List<LiveRoomBean> listLives = dao.find("select l from LiveRoomBean l where l.userId = ?1", userId);
        String str = listLives.get(0).getLiveUrl();
        String [] strArr = str.split("/");
        System.out.println(strArr[strArr.length-1]);
        LiveBean liveBean = new LiveBean();
        liveBean.setUserId(userId);
        liveBean.setUserName(username);
        liveBean.setLiveUrl(listLives.get(0).getLiveUrl());
        liveBean.setNum(listLives.get(0).getNum());
        liveBean.setImgUrl(imgUrl);
        liveBean.setLiveId(listLives.get(0).getLiveId());
        liveBean.setLiveName(liveName);
        liveBean.setStartTime(getTime());
        liveBean.setState("开播");
        liveBean.setType(type);
        LiveBean liveBean1 = dao.save(liveBean);
        List list = new ArrayList();
        list.add(liveBean1);
        JsonData jsonData = new JsonData();
        jsonData.setTotalCount(1L);
        jsonData.setData(list);
        return  jsonData;
    }

    /**
     * @Author lwj
     * @Description 获取第一个直播间
     * @Date 14:48 2019/2/22
     **/
    public JsonData  getFirstLive(){
        List<LiveBean> liveBean = dao.find("SELECT l FROM LiveBean l WHERE l.num = 1");
        if(liveBean.isEmpty()){
            liveBean = dao.find("SELECT l FROM LiveBean l WHERE l.state = '开播'");
            List list = new ArrayList();
            list.add(liveBean.get(0));
            JsonData jsonData = new JsonData();
            jsonData.setTotalCount(1L);
            jsonData.setData(list);
            return  jsonData;
        }
        JsonData jsonData = new JsonData();
        jsonData.setTotalCount(1L);
        jsonData.setData(liveBean);
        return  jsonData;
    }

    /**
     * @Author lwj
     * @Description 获取6个直播间
     * @Date 8:46 2019/2/23
     * @Update 14:22 2019/04/18
     **/
    public JsonData  getSixLive(){
        List<LiveBean> liveBean = dao.findByNativeSql("SELECT * FROM \"public\".\"mooc_live\" WHERE state='开播' ORDER BY num desc limit 6",LiveBean.class);
//  "SELECT * FROM \"public\".\"mooc_live\" WHERE 1<num and num<7 and state='开播' ORDER BY num",LiveBean.class
        JsonData jsonData = new JsonData();
//        if (liveBean.size() <=6){
        jsonData.setTotalCount((long)liveBean.size());
        jsonData.setData(liveBean);
//            int num = 6-liveBean.size();
//            List<LiveBean> liveBeans = dao.findByNativeSql("SELECT * FROM \"public\".\"mooc_live\" WHERE state='开播' LIMIT " + num,LiveBean.class);
//            jsonData.setData(liveBeans);
//            return  jsonData;
//        }
//        jsonData.setTotalCount(6L);
//        jsonData.setData(liveBean);
        return  jsonData;
    }

    /**
     * @Author lwj
     * @Description 关闭直播
     * @Date 下午 4:17 2019/1/23 0023
     **/
    public JsonData  closeLive(Long userId){
        List<LiveBean> liveBeanList = dao.find("select l from LiveBean l where l.userId = ?1", userId);
        for (int i = 0; i < liveBeanList.size(); i++) {
           Long id = liveBeanList.get(i).getId();
           dao.updateNativeQuery("UPDATE \"public\".\"mooc_live\" SET \"state\"='闭播' WHERE \"id\" = " + id);
        }
        JsonData jsonData = new JsonData();
        jsonData.setTotalCount(1L);
        List list = new ArrayList();
        Map map = new HashMap();
        map.put("flag","success");
        list.add(map);
        jsonData.setData(list);
        return  jsonData;
    }

    /**
     * @Author lwj
     * @Description 获取所有的直播间
     * @Date 14:34 2019/1/29
     **/
    public JsonData  getLiveAllUrl(String state,String type){
        JsonData jsonData = new JsonData();
        if (type == null) {
            List<LiveBean> liveBeanList = dao.find("select l from LiveBean l where l.state = ?1", state);
            jsonData.setTotalCount((long) liveBeanList.size());
            jsonData.setData(liveBeanList);
            return jsonData;
        }
        List<LiveBean> liveBeanList = dao.find("select l from LiveBean l where l.state = ?1 and l.type=?2",state,type);
        jsonData.setTotalCount((long) liveBeanList.size());
        jsonData.setData(liveBeanList);
        return jsonData;
    }

    /**
     * @Author lwj
     * @Description 根据直播间ID获取直播间
     **/
    public JsonData  getLive(Long liveId){
        List<LiveBean> liveBeanList = dao.find("select l from LiveBean l where l.liveId = ?1 ORDER BY l.startTime DESC ",liveId);
        JsonData jsonData = new JsonData();
        for (int i = 0; i < liveBeanList.size(); i++) {
            if("开播".equals(liveBeanList.get(0).getState())){
                jsonData.setTotalCount((long) liveBeanList.size());
                jsonData.setData(liveBeanList);
                return jsonData;
            }
        }
        List lists = new ArrayList();
        lists.add(liveBeanList.get(0));
        jsonData.setTotalCount((long) lists.size());
        jsonData.setData(lists);
        return jsonData;
    }

    /**
     * @Author lwj
     * @Description 获取分页数据
     * @Date 8:41 2019/4/3
     **/
    public JsonData getPageDate(Integer page, Integer limit, String type, String state){
        List<LiveBean> liveBeanList = dao.findByNativeSql("SELECT * FROM mooc_live  where type='"+type+"' and state='"+state+"'  LIMIT "+limit+" OFFSET "+page*limit+"",LiveBean.class);
        JsonData jsonData = new JsonData();
        List<LiveBean> liveBeanLists = dao.find("select l from LiveBean l where l.state = ?1 and l.type=?2",state,type);
        jsonData.setTotalCount((long) liveBeanLists.size());
        jsonData.setData(liveBeanList);
        return jsonData;
    }

    /**
     * @Author lwj
     * @Description 获取系统时间
     * @Date 10:13 2019/2/16
     **/
    public String getTime(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());// new Date()为获取当前系统时间
        return time;
    }
    /**
     * @Author yyp
     * @Description 重写方法，修改之前update num
     * @Date 16:35 2019/4/18
     * */
    public void beforeUpdateEntity(LiveBean entity, JsonStatus status){
        if (entity.getNum() != null) {
            Long num = entity.getNum();
            List<LiveBean> liveBeans = dao.getAll();
            for (int i = 0; i < liveBeans.size(); i++) {
                if(liveBeans.get(i).getNum() == num){
                    dao.updateNativeQuery("UPDATE \"public\".\"mooc_live\" SET num = '0' WHERE id = "+liveBeans.get(i).getId());
                    dao.updateNativeQuery("UPDATE \"public\".\"mooc_live_room\" SET num = '0' WHERE liveid = 123"+liveBeans.get(i).getLiveId());
                }
            }
        }
    }

}
